﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using Extjs_ReGSharp.Exceptions;
using Extjs_ReGSharp.Sorting.Factory;

namespace Extjs_ReGSharp.Sorting
{
	internal class DefaultSortingBehaviour<T> : ISortingBehaviour<T>
	{
		#region Data Memebers

		private TypePropertyWrapper<T> m_Property;

		private IDefaultComparer<T> m_Comparer;

		#endregion

		#region Constructor

		public DefaultSortingBehaviour(string fieldName)
		{
			m_Property = new TypePropertyWrapper<T>(fieldName);

			IComparisonFactory<T> factory = new ComparisonFactory<T>();

			m_Comparer = factory.GetComparerByType(m_Property.FieldType);
		}

		#endregion

		#region Private Methods


		#endregion

		#region Public Method

		public int Compare(T firstInstance, T secondInstance)
		{
			int ascendingResult = m_Comparer.Compare(firstInstance, secondInstance, m_Property.Property);

			return ascendingResult;
		}

		#endregion
	}
}














//private Expression<Func<T, TKey>> GetComparer<TKey>()
//        {
//            ParameterExpression objectBeingSortedParameter = Expression.Parameter(typeof(T), "sortedObject");

//            Expression<Func<T, TKey>> result =
//                Expression.Lambda<Func<T, TKey>>(Expression.Property(objectBeingSortedParameter, m_Property.Property.Name), objectBeingSortedParameter);

//            return result;
//        }

//        #endregion

//        #region Properties

//        public Expression<Func<T, TKey>> Comparer<TKey>()
//        {
//            Expression<Func<T, TKey>> result = GetComparer<TKey>();

//            return result;
//        }